package com.example.com.common.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.com.common.R;
import com.example.com.common.utils.SizeUtils;

import java.util.List;

/**
 * Created by qingning on 2016/3/31.
 */
public class ActionSheetDialog extends Dialog implements View.OnClickListener{
    int statusBarColor = 0x38000000;
    private LinearLayout mPanel;
    private String cancelText;
    private List<String> otherList;
    private static final int CANCEL_BUTTON_ID = R.id.btn_cancel;
    Drawable cancelButtonBackground;
    Drawable otherButtonTopBackground;
    Drawable otherButtonBottomBackground;
    Drawable otherButtonSingleBackground;
    int cancelButtonTextColor;
    int otherButtonTextColor;
    int padding;
    int otherButtonSpacing = 0;
    int cancelButtonMarginTop;
    DialogCallBack callback;
    public ActionSheetDialog(Context context, List<String> otherList, DialogCallBack callback) {
        super(context, R.style.bottomDialog);
        this.otherList = otherList;
        Resources res = context.getResources();
        padding = res.getDimensionPixelSize(R.dimen.activity_horizontal_margin);
        cancelButtonBackground = res.getDrawable(R.drawable.slt_as_ios7_cancel_bt);
        otherButtonTopBackground = res.getDrawable(R.drawable.slt_as_ios7_other_bt_top);
        otherButtonBottomBackground = res.getDrawable(R.drawable.slt_as_ios7_other_bt_bottom);
        otherButtonSingleBackground = res.getDrawable(R.drawable.slt_as_ios7_other_bt_single);
        cancelButtonTextColor = res.getColor(R.color.textcolor_gray);
        otherButtonTextColor = res.getColor(R.color.textcolor_gray_dark);
        cancelButtonMarginTop = SizeUtils.dp2px(10);
        this.callback = callback;
        cancelText = "取消";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(statusBarColor);
        }
        mPanel = new LinearLayout(getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mPanel.setLayoutParams(params);
        mPanel.setOrientation(LinearLayout.VERTICAL);
        mPanel.setGravity(Gravity.BOTTOM);
        setContentView(mPanel);
        mPanel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (canceledOnTouchOutside)
                    dismiss();
                return false;
            }
        });
        createItems();
    }

    private void createItems() {
        if (otherList != null) {
            for (int i = 0; i < otherList.size(); i++) {
                Button bt = new Button(getContext());
                bt.setId(CANCEL_BUTTON_ID + i + 1);
                bt.setOnClickListener(this);
                bt.setBackgroundDrawable(getOtherButtonBg(i));
                bt.setText(otherList.get(i));
                bt.setTextColor(otherButtonTextColor);
                bt.setTextSize(15);
                if (i > 0) {
                    LinearLayout.LayoutParams params = createButtonLayoutParams();
                    params.topMargin = otherButtonSpacing;
                    mPanel.addView(bt, params);
                } else {
                    mPanel.addView(bt);
                }
            }
        }
        Button bt = new Button(getContext());
        bt.getPaint().setFakeBoldText(true);
        bt.setTextSize(15);
        bt.setId(CANCEL_BUTTON_ID);
        bt.setBackgroundDrawable(cancelButtonBackground);
        bt.setText(cancelText);
        bt.setTextColor(cancelButtonTextColor);
        bt.setOnClickListener(this);
        LinearLayout.LayoutParams params = createButtonLayoutParams();
        params.topMargin = cancelButtonMarginTop;
        params.bottomMargin = cancelButtonMarginTop;
        mPanel.addView(bt, params);
        mPanel.setPadding(padding, 0, padding, 0);
    }
    private Drawable getOtherButtonBg(int i) {
        if (otherList.size() == 1) {
            return otherButtonSingleBackground;
        }
        if (otherList.size() == 2) {
            switch (i) {
                case 0:
                    return otherButtonTopBackground;
                case 1:
                    return otherButtonBottomBackground;
            }
        }
        if (otherList.size() > 2) {
            if (i == 0) {
                return otherButtonTopBackground;
            }
            if (i == (otherList.size() - 1)) {
                return otherButtonBottomBackground;
            }
            return getContext().getResources().getDrawable(R.drawable.slt_as_ios7_other_bt_middle);
        }
        return null;
    }
    public LinearLayout.LayoutParams createButtonLayoutParams() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        return params;
    }
    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() !=CANCEL_BUTTON_ID ) {
            if (callback != null) {
                callback.onClick(v.getId() - CANCEL_BUTTON_ID - 1);
            }
        }
    }

    private boolean canceledOnTouchOutside = true;
    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.canceledOnTouchOutside = cancel;
    }
}
